<template>
  <!-- 供应商账单 -->
  <el-container>
    <el-header>
      <div class="left-panel">
        <!-- <el-button
          type="primary"
          icon="el-icon-plus"
          @click="exportBill"
          v-auth-button="['export/vendorBill']"
          >导出</el-button
        > -->
      </div>
      <div class="right-panel">
        <div class="right-panel-search">
          <sc-table-select
            style="width: 100%"
            v-model="searchSelectVendor"
            :apiObj="apiObjVendor"
            :table-width="700"
            clearable
            collapse-tags
            collapse-tags-tooltip
            :props="props"
            :params="vendorForm"
            @change="changeVendor"
          >
            <template #header="{ form, submit }">
              <el-form :model="form">
                <el-form-item>
                  <el-input
                    v-model="form.search"
                    placeholder="供应商名称"
                    clearable
                    style="width: 100%"
                  ></el-input>
                </el-form-item>
                <el-button type="primary" @click="submit">查询</el-button>
              </el-form>
              <!-- <el-alert title="自定义FORM插糟 传递了form对象和提交方法" type="info" /> -->
            </template>
            <el-table-column
              prop="name"
              label="供应商名称"
              min-width="150"
            ></el-table-column>
            <el-table-column
              label="类型"
              prop="vendorCategoryName"
              width="200"
            ></el-table-column>
            <el-table-column
              label="电话"
              prop="telephone"
              width="120"
            ></el-table-column>
          </sc-table-select>
          <el-date-picker
            v-model="searchValue.startTime"
            type="date"
            placeholder="选择开始日期"
            value-format="YYYY-MM-DD"
            style="width: 100%"
          >
          </el-date-picker>
          <el-date-picker
            v-model="searchValue.endTime"
            type="date"
            placeholder="选择结束日期"
            value-format="YYYY-MM-DD"
            style="width: 100%"
          >
          </el-date-picker>
          <el-button
            type="primary"
            icon="el-icon-search"
            @click="upsearch"
          ></el-button>
        </div>
      </div>
    </el-header>
    <div class="count">
      <p>欠款总额：{{ countMoney.totalVendorMoney }}元</p>
      <p>付款总额：{{ countMoney.paymentVendorMoney ?? 0 }}元</p>
    </div>
    <el-main class="nopadding">
      <scTable
        ref="table"
        :apiObj="apiObj"
        row-key="id"
        stripe
        @row-dblclick="handleRowDblclick"
        :showSummary="true"
        :summary-method="getSummaries"
      >
        <!-- <el-table-column label="ID" prop="id" width="50"></el-table-column> -->
        <el-table-column
          label="供应商名称"
          prop="vendorName"
          min-width="250"
          :show-overflow-tooltip="true"
        ></el-table-column>
        <el-table-column label="类型" prop="type" min-width="100">
        </el-table-column>
        <el-table-column label="金额" prop="money" min-width="100">
          <template #default="scope">
            <p>{{ scope.row.money }}元</p>
          </template>
        </el-table-column>
        <el-table-column label="日期" prop="createTime" min-width="100">
        </el-table-column>
      </scTable>
    </el-main>
  </el-container>
</template>

<script>
import { exportFile } from "@/utils/exportFile"
export default {
  name: "vendorBillList",
  data () {
    return {
      apiObj: this.$API.reportForm.getVendorBillList,
      searchValue: {
        vendorId: null,
        startTime: "",
        endTime: "",
      },
      apiObjVendor: this.$API.vendor.selectVendorList,
      vendorForm: {
        vendorId: null,
        startTime: null,
        endTime: null,
      },
      props: {
        label: "name",
        value: "id",
      },
      searchSelectVendor: {},
      columnsFilters: ["money"],
      countMoney: {},
    }
  },
  mounted () {
    this.getCountMoney()
  },
  methods: {
    //统计
    async getCountMoney () {
      let { data } = await this.$API.reportForm.getVendorPlanMoney.get()
      this.countMoney = data || {}

    },
    //导出
    async exportBill () {
      let res = await this.$API.reportForm.exportVendorBill.get(this.searchValue)
      exportFile(res, '供应商账单.xls')
    },
    //合计行统计
    getSummaries (param) {
      console.log(param)
      const { columns, data } = param
      const sums = []
      columns.forEach((column, index) => {
        if (index === 0) {
          sums[index] = "页合计"
          return
        }
        if (this.columnsFilters.includes(column.property)) {
          const values = data.map((item) =>
            Number(item[column.property])
          )
          if (!values.every((value) => isNaN(value))) {
            sums[index] = values.reduce((prev, curr) => {
              const value = Number(curr)
              if (!isNaN(value)) {
                return prev + curr
              } else {
                return prev
              }
            }, 0)
            sums[index] += " 元"
          } else {
            sums[index] = ""
          }
        }
      })
      return sums
    },
    changeVendor (val) {
      this.searchValue.vendorId = val.id
      console.log(this.searchValue)
    },
    //搜索
    upsearch () {
      this.$refs.table.upData(this.searchValue)
    },

    //本地更新数据
    handleSaveSuccess () {
      this.$refs.table.refresh()
    },

    // 双击表格
    handleRowDblclick (row) {
      this.table_show(row)
    },
  },
};
</script>

<style lang="scss" scoped>
.count {
  display: flex;
  align-items: center;
  justify-content: flex-end;
  height: 50px;
  padding-left: 12px;
  font-size: 14px;
  color: #555;
  font-weight: 600;
  p {
    margin-right: 20px;
  }
}
</style>
